package com.ll.beautymall.ui.home;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.RadioGroup;

import com.ll.beautymall.R;
import com.ll.beautymall.ui.home.homefragment.view.HomeFragment;
import com.ll.beautymall.utils.FragmentUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/*
 * 第一层Fragment切换
 */
public class MainActivity extends AppCompatActivity {

    @BindView(R.id.rdg_all)
    RadioGroup rdgAll;
    private int currentIndex = 0;
    private List<Fragment> fragments;
    private Unbinder bind;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //解决当activity被回收后，Fragment穿透问题
//        if (savedInstanceState != null) {
//            for (int i = 0; i < 5; i++) {
//                Fragment savedFragment = getSupportFragmentManager().findFragmentByTag(String.valueOf(i));
//                if (savedFragment != null) {
//                    fragments.add(savedFragment);
//                }
//            }
//        }
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        bind = ButterKnife.bind(this);
        //默认加载第一个
        getSupportFragmentManager().beginTransaction().add(R.id.rl_containter, new HomeFragment()).commit();
        initView();
    }


    public void initView() {
        fragments = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Fragment fragment = FragmentUtil.createFragment(i);
            fragments.add(fragment);
        }
        rdgAll.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    if (radioGroup.getChildAt(i).getId() == checkedId) {
                        getSupportFragmentManager().beginTransaction().replace(R.id.rl_containter, FragmentUtil.createFragment(i)).commit();
//                        switchFragment(i);
                    }
                }
            }
        });
    }

    private void switchFragment(int i) {
        if (i == currentIndex) {
            return;
        }
        Fragment targetFragment = fragments.get(i);
        Fragment currentFragment = fragments.get(currentIndex);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (targetFragment.isAdded()) {
            transaction.hide(currentFragment).show(targetFragment);
        } else {
            transaction.hide(currentFragment).add(R.id.rl_containter, targetFragment, String.valueOf(i));
        }
        transaction.commit();
        currentIndex = i;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bind != null) {
            bind.unbind();
        }
    }

//    @Override
//    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
//    }

}
